import pandas as pd
import seaborn as sns
import plotly.express as px
import matplotlib.pyplot as plt
from plotly.offline import init_notebook_mode
init_notebook_mode(connected=True)
import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"
For this excercise, we have written the following code to load the stock dataset built into plotly express.
stocks = px.data.stocks()
stocks.head()
| date | GOOG | AAPL | AMZN | FB | NFLX | MSFT | |
|---|---|---|---|---|---|---|---|
| 0 | 2018-01-01 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |
| 1 | 2018-01-08 | 1.018172 | 1.011943 | 1.061881 | 0.959968 | 1.053526 | 1.015988 |
| 2 | 2018-01-15 | 1.032008 | 1.019771 | 1.053240 | 0.970243 | 1.049860 | 1.020524 |
| 3 | 2018-01-22 | 1.066783 | 0.980057 | 1.140676 | 1.016858 | 1.307681 | 1.066561 |
| 4 | 2018-01-29 | 1.008773 | 0.917143 | 1.163374 | 1.018357 | 1.273537 | 1.040708 |
Select a stock and create a suitable plot for it. Make sure the plot is readable with relevant information, such as date, values.
new_stocks = stocks.set_index("date")
plt.figure(figsize=(12,8))
new_stocks["GOOG"].plot()
plt.title("Google stock")
plt.xlabel("date")
plt.ylabel("stock value")
plt.show()
You've already plot data from one stock. It is possible to plot multiples of them to support comparison.
To highlight different lines, customise line styles, markers, colors and include a legend to the plot.
plt.figure(figsize=(12,8))
for c in new_stocks.columns:
new_stocks[c].plot(label=c)
plt.title("Stock")
plt.xlabel("date")
plt.ylabel("stock value")
plt.legend()
plt.show()
First, load the tips dataset
tips = sns.load_dataset('tips')
tips.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
Let's explore this dataset. Pose a question and create a plot that support drawing answers for your question.
Some possible questions:
# YOUR CODE HERE
plt.figure(figsize=(10,3))
plt.subplot(1,2,1)
sns.scatterplot(x='tip', y='total_bill', data=tips[tips["sex"]=='Male'])
plt.xlim(0,11)
plt.ylim(0,55)
plt.title('male')
plt.subplot(1,2,2)
sns.scatterplot(x='tip', y='total_bill', data=tips[tips["sex"]=='Female'])
plt.xlim(0,11)
plt.ylim(0,55)
plt.title('female')
plt.show()
# OR:
sns.boxplot(x='sex', y='tip', data=tips)
plt.show()
Putting the outliers aside, it seems that women tend to tip a bit less, but all in all the same as men.
sns.heatmap(tips.corr(), cmap='YlGn', annot=True)
plt.show()
Based on the graph above we can conclude that total_bill correlates the most with tip. Because the correlation is 0.68.
Redo the above exercises (challenges 2 & 3) with plotly express. Create diagrams which you can interact with.
Hints:
modified_stocks = pd.melt(stocks, id_vars=['date'], var_name='name', value_name='value')
modified_stocks.head()
| date | name | value | |
|---|---|---|---|
| 0 | 2018-01-01 | GOOG | 1.000000 |
| 1 | 2018-01-08 | GOOG | 1.018172 |
| 2 | 2018-01-15 | GOOG | 1.032008 |
| 3 | 2018-01-22 | GOOG | 1.066783 |
| 4 | 2018-01-29 | GOOG | 1.008773 |
fig = px.line(modified_stocks, x='date', y='value', color='name', symbol='name')
fig.show()
fig = px.scatter(tips, x='total_bill', y='tip', color='sex', facet_col='smoker', facet_row='time')
fig.show()
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
#load data
df = px.data.gapminder()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
pop = df.query("year == 2007")
pop_new = pop.groupby('continent').sum()
fig = px.bar(pop_new, x='pop', y=pop_new.index, color=pop_new.index, text_auto='.2s')
fig.update_yaxes(categoryorder = "total ascending")
fig.update_traces(textposition='outside')
fig.update_layout(showlegend=False)
fig.show()